<?php

class Page {
	
	public $page_title;
	public $page_name = false;
	public $db;
	public $html;
	public $template;
	public $error;
	public $settings;
	public $layout = 'default';
	public $user;
	private $content = array();
	public $include_fb = false;
	
	public function load(){
		
	}
	
	public function __construct(){
		global $error, $db, $settings, $user;
		
		$this->db =& $db;
		
		$this->html = new html;

		$this->template = new SmartySettings;
		
		$this->user =& $user;
		
		$this->error =& $error;
		
		$this->settings =& $settings; 
		
		$this->template->assign_array(array('settings' => $this->settings->data));
		
	}
	
	public function display(){

		$this->load();		
		$err = $this->error->show();

		$this->layout = ($err) ? "e{$err}_page" : $this->layout;
		$this->html->cache = $this->settings->data["external_caching"];
	
		if($this->settings->data["integrate_facebook"])
			$this->addFbCode();
	
		if($this->page_name)
			$this->content[] = "{include file='{$this->page_name}_page.tpl'}";
	
		$this->template->assign_array($this->html->display());	
		$this->template->assign_array(array(
			'page_title' => $this->page_title,
			'content' => implode("\n", $this->content),
			'page_name' => $this->page_name,
			'user' => array(
				'loggedin' => $this->user->isLoggedin(),
				'founder'  => $this->user->isFounder(),
				'data' => $this->user->data)
		));

		if($this->layout)
			$this->template->display($this->layout . '.tpl');
		
	} 
		
	public function addContent($ct = false){
		if($ct)
			$this->content[] = $ct;
	}
	
	private function addFbCode()
	{
		$this->html->js("
		window.fbAsyncInit = function() {
			FB.JSON.stringify = function (value) { return JSON.encode(value);};
	     FB.init({
	       appId   : '" . $this->user->fb->getAppId() ."',
	       session : ". json_encode($this->user->session).", // don't refetch the session when PHP already has it
	       status  : true, // check login status
	       cookie  : true, // enable cookies to allow the server to access the session
	       xfbml   : true // parse XFBML
	     });

	     //whenever the user logs in, we refresh the page
	     FB.Event.subscribe('auth.login', function() {
	       userstatus.updateStatus();
	     });
	   };

	   (function() {
			if(!document.getElementById('fb-root'))
				return;
       var e = document.createElement('script');
       e.src = document.location.protocol + '//connect.facebook.net/hu_HU/all.js';
       e.async = true;
	     document.getElementById('fb-root').appendChild(e);
	   }());
		", array('source' => true));
	}
	
}

?>